﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace RAD.LightOne.Web.Security
{
    #region Documentation
    /// <summary>
    /// Classe que encapsula o modelo de dados de usuários.
    /// </summary>
    #endregion
    [ClassName("Class")] //class_name
    [LastPersistence("Register")] //last_persistence
    [DBTable("Users")]
    [Serializable]
    internal sealed class MembershipUserModel : MyObject<MembershipUserModel>, IFull<MembershipUserModel>
    {
        #region Fields

        [DBField("password", DataOperations = DataOperations.Insert)]
        private string password;
        [DBField("passwordAnswer", DataOperations = DataOperations.Insert)]
        private string passwordAnswer;

        #endregion

        #region Properties
        [DBField("Username")]
        public string UserName { get; set; }
        [DBField("ApplicationName")]
        public string Application { get; set; }
        [DBField("Comment")]
        public string Comment { get; set; }
        [DBField("CreationDate")]
        public DateTime CreationDate { get; set; }
        [DBField("email")]
        public string Email { get; set; }
        [DBField("IsApproved")]
        public bool IsApproved { get; set; }
        [DBField("IsLockedOut")]
        public bool IsLockedOut { get; set; }
        [DBField("IsOnline")]
        public bool IsOnline { get; set; }
        [DBField("LastActivityDate")]
        public DateTime LastActivityDate { get; set; }
        [DBField("LastLockedOutDate")]
        public DateTime LastLockoutDate { get; set; }
        [DBField("LastLoginDate")]
        public DateTime? LastLoginDate { get; set; }
        [DBField("LastPasswordChangedDate")]
        public DateTime LastPasswordChangedDate { get; set; }
        [DBField("PasswordQuestion")]
        public string PasswordQuestion { get; set; }

        public string ProviderName
        {
            get;
            set;
        }

        #endregion

        #region Constructors
        internal MembershipUserModel() : base() { }

        internal MembershipUserModel(
            string username,
            string email,
            string passwordQuestion,
            string comment,
            bool isApproved,
            bool isLockedOut,
            DateTime creationDate,
            DateTime? lastLoginDate,
            DateTime lastActivityDate,
            DateTime lastPasswordChangedDate,
            DateTime lastLockedOutDate,
            //fields
            string passwordAnswer,
            string password,
            string applicationName
            )
            : base()
        {
            this.UserName = username;
            this.Email = email;
            this.PasswordQuestion = passwordQuestion;
            this.Comment = comment;
            this.IsApproved = isApproved;
            this.IsLockedOut = isLockedOut;
            this.CreationDate = creationDate;
            this.LastLoginDate = lastLoginDate;
            this.LastActivityDate = lastActivityDate;
            this.LastPasswordChangedDate = lastPasswordChangedDate;
            this.LastLockoutDate = lastLockedOutDate;
            this.password = password;
            this.passwordAnswer = passwordAnswer;
            this.Application = applicationName;
        }

        #endregion

        #region Methods

        public static MembershipUserModel FindByEmail(string email)
        {
            return MembershipUserModel.FindBySql("select Id, Class from Users where Email={0}", email).FirstOrDefault();
        }

        [Obsolete("", true)]
        public static MembershipUserModel FindByUserName(string userName)
        {
            return MembershipUserModel.FindBySql("select Id, Class from Users where Username={0}", userName).FirstOrDefault();
        }

        public static MembershipUserModel FindByUserName(string userName, string applicationName)
        {
            return MembershipUserModel.FindBySql("select Id, Class from Users where Username={0} and applicationName = {1}", userName, applicationName).FirstOrDefault();
        }

        public static MembershipUserModel FindByUserNameAnApplication(string userName, string application)
        {
            return MembershipUserModel.FindBySql("select Id, Class from Users where Username={0} and applicationName = {1}", userName, application).FirstOrDefault();
        }

        public new void Save()
        {
            base.Save();
        }

        public new void Save(IDbTransaction trx)
        {
            base.Save(trx);
        }

        public new void Load()
        {
            base.Load();
        }

        public new void Load(IDbTransaction trx)
        {
            base.Load(trx);
        }

        public new void Delete()
        {
            base.Delete();
        }

        public new void Delete(IDbTransaction trx)
        {
            base.Delete(trx);
        }

        #endregion

        #region Operators

        #region Documentation
        /// <summary>
        /// Conversão implicita do container de dados am classe ne negócio.
        /// </summary>
        #endregion
        public static implicit operator LightOneMembershipUser(MembershipUserModel model)
        {
            if (model == null) return null;
            return new LightOneMembershipUser(model);
        }

        #endregion
    }
}
